package com.mindainfo.configCenter.base.dao.impl;

import com.mindainfo.configCenter.base.bean.MongoConfig;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoClientURI;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.gridfs.GridFSBucket;
import com.mongodb.client.gridfs.GridFSBuckets;
import com.mongodb.client.gridfs.GridFSDownloadStream;
import com.mongodb.client.gridfs.GridFSUploadStream;
import com.mongodb.client.gridfs.model.GridFSUploadOptions;
import com.sun.corba.se.spi.orbutil.fsm.Input;
import org.apache.commons.io.IOUtils;
import org.bson.types.ObjectId;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.Morphia;
import org.springframework.stereotype.Repository;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by MD-01 on 2016/11/14.
 */
@Repository
public class MorphiaDao {

    private MongoClient mongoClient;
    private Datastore datastore;

    @Resource
    private MongoConfig config;

    @PostConstruct
    public void init(){
        MongoClientURI mongoClientURI = new MongoClientURI(config.getUri());
        mongoClient = new MongoClient(mongoClientURI);

        MongoClientOptions options = mongoClient.getMongoClientOptions();
        System.err.println(options);

        Morphia morphia = new Morphia();
        morphia.mapPackage("com.mindainfo.**");
        datastore = morphia.createDatastore(mongoClient, config.getDatabase());
        datastore.ensureIndexes();
    }

    public MongoClient getMongoClient(){
        return mongoClient;
    }
    public Datastore getDatastore(){
        return datastore;
    }

    public String upload(String fileName, GridFSUploadOptions option, InputStream is) {
        MongoDatabase db = getMongoClient().getDatabase(config.getDatabase());
        GridFSBucket bucket = GridFSBuckets.create(db);
        GridFSUploadStream os;

        if (option != null) {
            os = bucket.openUploadStream(fileName, option);
        } else {
            os = bucket.openUploadStream(fileName);
        }
        try {
            os.write(IOUtils.toByteArray(is));
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    is = null;
                }
            }
        }
        ObjectId id = os.getFileId();
        os.flush();
        os.close();
        return id.toString();
    }

    /**
     * @title	保存文件到mongodb
     * @param fileName	文件名
     * @param option	可选说明
     * @param data		字节数组(数据)
     * @return
     * @author	陈义龙
     * @date	2016年12月7日 下午5:04:50
     */
    public String upload(String fileName, GridFSUploadOptions option, byte[] data) {
        MongoClient client = getMongoClient();
        MongoDatabase db = client.getDatabase(config.getDatabase());
        GridFSBucket bucket = GridFSBuckets.create(db);
        GridFSUploadStream os;

        if (option != null) {
            os = bucket.openUploadStream(fileName, option);
        } else {
            os = bucket.openUploadStream(fileName);
        }
        os.write(data);
        ObjectId id = os.getFileId();
        os.flush();
        os.close();
        return id.toString();
    }

    public GridFSDownloadStream getInputStream(String id){
        MongoClient client = getMongoClient();
        MongoDatabase db = client.getDatabase(config.getDatabase());
        GridFSBucket bucket = GridFSBuckets.create(db);
        GridFSDownloadStream gridFSDownloadStream = bucket.openDownloadStream(new ObjectId(id));
        return gridFSDownloadStream;
    }

    public void deleteFile(String id){
        MongoClient client = getMongoClient();
        MongoDatabase db = client.getDatabase(config.getDatabase());
        GridFSBucket bucket = GridFSBuckets.create(db);
        bucket.delete(new ObjectId(id));
    }
}
